﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_DataSourceSelect : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.ddlColumn.Attributes.Add("onchange", "if(!checkSelect()){return false;}");    //选择验证

        if (!IsPostBack)
        {
            BindDrop();
        }

        setStyle();
    }

    //绑定下拉
    protected void BindDrop()
    {
        this.ddlColumn.DataValueField = "columnID";
        this.ddlColumn.DataTextField = "columnName";
        Whir.Common.BindHelper.BindDropDownList(this.ddlColumn, "Whir_Column", "parentID=", "0", "", "－ 栏目列表 －");
    }

    //样式设置
    protected void setStyle()
    {
        //将不可选项置灰
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("columnID", "Whir_Column", "formID='-1'", "");
        int c = this.ddlColumn.Items.Count;
        for (int i = 0; i < c; i++)
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (this.ddlColumn.Items[i].Value == dr[0].ToString())
                {
                    this.ddlColumn.Items[i].Attributes.CssStyle.Value = "color:#999999;";
                    break;
                }
            }
        }
    }

    //绑定字段和表名
    public void BindData()
    {
        string tableName, primarykeyName;
        string cid = this.ddlColumn.SelectedValue;
        this.lbColumnID.Text = "TypeID=" + cid;

        //表名
        string sql1 = "select Whir_Model.tableName from Whir_Column,Whir_Model where Whir_Column.columnID=" + cid + " and Whir_Column.modelID=Whir_Model.modelID";
        object obj = Whir.SQLServerDAL.DbHelperSQL.GetSingle(sql1);
        if (obj == null)
            return;
        tableName = obj.ToString(); //取得表名
        primarykeyName = ContentManage.GetCustomTablePrimaryKeyName(tableName); //取得该表主键的格式
        this.hfTable.Value = tableName;
        //end

        string sql2 = "select formID from Whir_Column where columnID=" + cid;
        string formId = Whir.SQLServerDAL.DbHelperSQL.GetSingle(sql2).ToString();
        int index = formId.IndexOf('|');
        if (index != -1)
            formId = formId.Split(',')[0].Substring(index + 1);

        string ids = Whir.SQLServerDAL.DbHelperSQL.GetSingle("select REPLACE(REPLACE(fieldIDs,'|true',''),'|false','') from Whir_ModelForm where modelFormID=" + formId).ToString();
        string sql3 = "select fieldName,alias from Whir_Field where fieldID in (" + ids + ")";
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(sql3).Tables[0];
        int count = dt.Rows.Count;

        this.cblField.Items.Clear();
        //添加一个主键字段到checkboxlist中
        this.cblField.Items.Add(new ListItem(primarykeyName + " " + "自动编号ID", primarykeyName));
        //添加其他字段
        for (int i = 0; i < count; i++)
        {
            this.cblField.Items.Add(new ListItem(dt.Rows[i]["fieldName"].ToString() + " " + dt.Rows[i]["alias"].ToString(), dt.Rows[i]["fieldName"].ToString()));
        }
        //绑定的时候默认选择主键、CreateDate发布字段、TypeID栏目ID
        foreach (ListItem item in this.cblField.Items)
        {
            if (item.Value == primarykeyName || item.Value.ToLower() == "createdate" || item.Value.ToLower() == "typeid")
            {
                item.Selected = true;
                item.Text += "  (* 必填)";
            }
            else
            {
                item.Text += "  <a onclick=\"insertColumnName('{$" + item.Text.Split(' ')[0] + "}')\">插入</a>";
            }
        }
    }

    //事件
    protected void ddlColumn_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
}
